﻿using AutoMapper;
using B.S.Traceability.Domain;
using B.S.Traceability.Error;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Read.API.Application.Commends.Farm;
using B.S.Traceability.Read.API.Dto;
using MediatR;

namespace B.S.Traceability.Read.API.Application.CommendHandlers.Farm
{
    public class GetFarmsByBatchIdCommendHandlers : IRequestHandler<GetFarmsByBatchIdCommends, APIResult<List<ProductsShoopDtos>>>
    {

        private readonly IBaseRepository<Farms> farmRepository;
        private readonly IBaseRepository<Shoops> shoopsRepository;
        private readonly IBaseRepository<Products> productsRepository;
        private readonly IBaseRepository<IntegrateImgs>   integrateRepository;
        private readonly IMapper mapper;

        public GetFarmsByBatchIdCommendHandlers(IBaseRepository<Farms> farmRepository, IBaseRepository<Shoops> shoopsRepository, IBaseRepository<Products> productsRepository, IBaseRepository<IntegrateImgs> integrateRepository, IMapper mapper)
        {
            this.farmRepository = farmRepository;
            this.shoopsRepository = shoopsRepository;
            this.productsRepository = productsRepository;
            this.integrateRepository = integrateRepository;
            this.mapper = mapper;
        }






        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<ProductsShoopDtos>>> Handle(GetFarmsByBatchIdCommends request, CancellationToken cancellationToken)
        {
            APIResult<List<ProductsShoopDtos>> result = new APIResult<List<ProductsShoopDtos>>();
            result.Code = APIEnum.查询成功;
            result.Msg= "查询成功";


            result.Code = APIEnum.查询成功;
            result.Msg = "查询成功";

            var products = productsRepository.GetAllAsync().Result;
            var shoops = shoopsRepository.GetAllAsync().Result;
  

            var lists = from a in products
                        join b in shoops on a.ShoopId equals b.ShoopsId
                      
                        where a.BatchId == request.BatchId
                        select new ProductsShoopDtos
                        {
                            ProductsId = a.ProductsId,
                            ShoopId = a.ShoopId,
                            BatchId = a.BatchId,
                            Shelflife = a.Shelflife,
                            Productionnumber = a.Productionnumber,
                            Manufacturers = a.Manufacturers,
                            Productsimg = a.Productsimg,
                            CreateName = a.CreateName,
                            CreateTime = a.CreateTime,
                            UpdateName = a.UpdateName,
                            UpdateTime = a.UpdateTime,
                            IsDel = a.IsDel,

                            ShoopsName = b.ShoopsName,
                         
                        };

            var data= mapper.Map<List<ProductsShoopDtos>>(lists);

            result.data=data.ToList() ;


            return Task.FromResult(result);
        }
    }
}
